<?php

namespace Xrfg\Core\Trading\Spot\Observers;

use Xrfg\Core\Common\Services\Proof;
use Xrfg\Core\Trading\Spot\Models\SpotAccount;
use Xrfg\Core\Trading\Spot\Models\SpotAccountCapitalflow;

class SpotAccountObserver
{
    public function updated(SpotAccount $account)
    {
        if ($account->availableChange || $account->disabledChange) {
            SpotAccountCapitalflow::create([
                'user_id' => $account->user_id,
                'coin_id' => $account->coin_id,
                'available_before' => $account->getOriginal('available'),
                'available_number' => $account->availableChange ?: 0,
                'available_after' => $account->available,
                'disabled_before' => $account->getOriginal('disabled'),
                'disabled_number' => $account->disabledChange ?: 0,
                'disabled_after' => $account->disabled,
                'certificate' => Proof::encode($account->proofId[0], $account->proofId[1], $account->proofId[2]),
                'proof_id' => $account->proofId[0],
                'class_id' => $account->proofId[1],
                'op_id' => $account->proofId[2],
                'version' => $account->version,
            ]);
        }
    }
}
